#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the version 3 of the GNU Lesser General Public License
#   as published by the Free Software Foundation.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Copyright (c) NEC Deutschland GmbH, NEC HPC Europe
#
# $Id$

import os

from aggmon.databases.metric.time_series_database import TimeSeriesDatabase, S_IN_DAY
from basic_types import NumRecord


__all__ = ["RRY"]


# Constants
S_IN_YEAR = 365 * S_IN_DAY
WIDTH_RRY = 1000


class RRY(TimeSeriesDatabase):
    """
    A RRY (Round Robin Year Archive) contains condensed numeric-typed metrics.
    """

    def __init__( self, dir_path, keep_exact=-1, rotation_period=S_IN_YEAR, width=WIDTH_RRY, *args ):
        super( RRY, self ).__init__( dir_path, keep_exact=keep_exact, rotation_period=rotation_period, *args )
        self.__width = width

    def append( self, time_ns, value ):
        assert False, "Usage error. Append is not possible!"

    def cascade( self, int_file_list ):
        """
        Condense files.
        """
        import logging
        # condensing on this level means files are obsolete and should be removed
        for file in int_file_list:
            # remove RRY file
            os.unlink( file )
            logging.debug( "RRY.cascade(): removed file " + file )

    def fetch_all( self, from_ns=0, to_ns=0, nsteps=0, step_ns=0, selected_files=None ):
        """
        This function overrides fetch_all in TimeSeriesDatabase to read RRY files.
        """
        records = super( RRY, self ).fetch_all( from_ns, to_ns, nsteps, step_ns, selected_files )
        return records

    def getWidth( self ):
        return self.__width

    def insert( self, time_ns, value ):
        self.insert_record( NumRecord( time_ns, value ) )

    def record( self ):
        return NumRecord()
    
    def _write_ts_info( self ):
        pass


from basic_types.factory import Factory
Factory.set_factory( "RRY", RRY, __name__ )
